
//
//  DetailPcViewController.m
//  EcmDoctor
//
//  Created by Mac on 16/2/25.
//  Copyright © 2016年 张进. All rights reserved.
//

#import "DetailPcViewController.h"
#import "PdcTableViewCell.h"
#import "PDC.h"
#import "UIImageView+AFNetworking.h"
#import "ImgaeViewController.h"
#import "AppUtils.h"
#import "TreatmentsViewController.h"
#import "ClinicDetailViewController.h"
#import "QuestionTelDetailController.h"
#import "QuestionOnlineDetailController.h"
#import "QuestionDetailController.h"

@interface DetailPcViewController ()<UITableViewDataSource,UITableViewDelegate,myProtocol1>
{

    __weak IBOutlet UITableView *_detailpcTv;
    NSMutableArray *_numAry;

    NSArray *_askStyAry;
}
@end

@implementation DetailPcViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;

    _askStyAry=@[@"免费义诊",@"在线问诊",@"电话问诊",@"门诊加号"];
    //计算得到cell的高度
    _numAry=[NSMutableArray array];
    for(int i=0;i<_dataAry.count;i++)
    {
        PDC *pdc=_dataAry[i];
        float a=pdc.pic.count*210+83+15+[self labelwidthWith:[NSString stringWithFormat:@"备注：%@",pdc.remark]]+1;
        NSNumber *num=[NSNumber numberWithFloat:a];
        [_numAry addObject:num];
    }
    // Do any additional setup after loading the view.
    self.navigationItem.titleView=[AppUtils generateNavigationItemTitleViewWithTitle:@"患者病历处方详情"];
    UIImage *tableIcon = [UIImage imageNamed:@"btn_back"];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithImage:tableIcon style:UIBarButtonItemStylePlain target:self action:@selector(changeToNavRootController:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithPatternImage:tableIcon];
   
   
}
-(void)changeToNavRootController:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}



-(CGFloat)labelwidthWith:(NSString *)str
{
    
    UIFont *font= [UIFont systemFontOfSize:15.0];
    NSDictionary *fontDic = @{NSFontAttributeName:font};
    
    CGRect labelRect = [str boundingRectWithSize:CGSizeMake(kScreenWidth-60,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
    CGSize size = labelRect.size;
    return size.height;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataAry.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSNumber *num=_numAry[indexPath.row];
    return num.floatValue;
    

}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    
    PDC *pdc=_dataAry[indexPath.row];
    PdcTableViewCell *pdcCell=[tableView dequeueReusableCellWithIdentifier:@"detailPc" forIndexPath:indexPath];
    pdcCell.delegate=self;
        pdcCell.qutime.text=[NSString stringWithFormat:@"问诊时间：%@",pdc.cases_date];
//    for(int i=0;i<_askStyAry.count;i++)
//    {
//        if([pdc.type isEqualToString:[NSString stringWithFormat:@"%d",i]])
//             pdcCell.qutype.text=_askStyAry[i];
//    }
    pdcCell.qutype.text=_askStyAry[pdc.type.intValue];
    for(int i=0;i<pdc.pic.count;i++)
    {
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectMake(15, 15+83+210*i,kScreenWidth-30, 200)];
         [imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://ecm-user.oss-cn-beijing.aliyuncs.com/image/%@",pdc.pic[i]]] placeholderImage:[UIImage imageNamed:@"icon"]];
        imageView.userInteractionEnabled=YES;
      [imageView  addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)]];
        [pdcCell addSubview:imageView];
    }
    
        pdcCell.bzViewheight.constant=[self labelwidthWith:[NSString stringWithFormat:@"备注：%@",pdc.remark]]+1;
    pdcCell.askid=pdc.ask_id;
        pdcCell.bzLabel.text=[NSString stringWithFormat:@"备注：%@",pdc.remark];
    cell=pdcCell;
    return cell;
}

-(void)treatments:(NSArray *)ary
{
  [self performSegueWithIdentifier:@"treatments" sender:ary];
}

-(void)tapView:(UITapGestureRecognizer*)tap
{
    ImgaeViewController *imageVc=[[ImgaeViewController alloc]init];
    imageVc.image=((UIImageView*)tap.view).image;
    [self.navigationController pushViewController:imageVc animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        
        //需要注意的是self.isViewLoaded是必不可少的，其他方式访问视图会导致它加载 ，在WWDC视频也忽视这一点。
        
        if (self.isViewLoaded && !self.view.window)// 是否是正在使用的视图
        {
            // Add code to preserve data stored in the views that might be
            // needed later.
            
            // Add code to clean up other strong references to the view in
            // the view hierarchy.
            self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
        }
        
    }
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([segue.identifier isEqualToString:@"treatments"]) {
        TreatmentsViewController* treatments = [segue destinationViewController];
        
        treatments.dataAry  =[NSArray arrayWithArray:sender];
        treatments.single=NO;
        treatments.i = 2;
        
    } else if ([segue.identifier isEqualToString:@"toClinic"]) {
        
        ClinicDetailViewController *clinic = [segue destinationViewController];
        clinic.ifDownload = 1;
        clinic.askId = sender;
    } else if ([segue.identifier isEqualToString:@"toYiZhen"]) {
        
        QuestionDetailController *detail = [segue destinationViewController];
        detail.ifDownload = 1;
        detail.askId = sender;
        
    } else if ([segue.identifier isEqualToString:@"toOnline"]) {
        
        QuestionOnlineDetailController *onLine = [segue destinationViewController];
        onLine.ifDownload = 1;
        onLine.askId = sender;
        
    } else if ([segue.identifier isEqualToString:@"toTel"]) {
        
        QuestionTelDetailController *tel = [segue destinationViewController];
        tel.ifDownload = 1;
        tel.askId = sender;
    }

    
}


@end
